package com.sdh.onlinemarket.common.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdh.onlinemarket.entity.Item;
import com.sdh.onlinemarket.entity.Orderlist;
import com.sdh.onlinemarket.entity.Orders;
import com.sdh.onlinemarket.entity.Sells;
import com.sdh.onlinemarket.service.ItemService;
import com.sdh.onlinemarket.service.OrderlistService;
import com.sdh.onlinemarket.service.OrdersService;
import com.sdh.onlinemarket.service.SellsService;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
//定时任务
public class Jobs {
    @Autowired
    OrdersService ordersService;
    @Autowired
    OrderlistService orderlistService;
    @Autowired
    ItemService itemService;
    @Autowired
    SellsService sellsService;

    boolean firstUpdate = false;
    @Scheduled(fixedDelay = 10000)
    //测试为1分钟更新一次销量
    public void fixedDelayJob() throws InterruptedException {
        System.out.println("fixedDelay 每隔1分钟" + new Date());

        if(!firstUpdate){
            //执行初始更新
            sellsService.remove(new QueryWrapper<>());
//            LocalDateTime localDateTime = LocalDateTime.now();
            List<Orders> ordersList = ordersService.list(new QueryWrapper<Orders>().orderByAsc("ordertime"));
            List<Item> itemList = itemService.list(new QueryWrapper<Item>().groupBy("itemid"));
            LocalDate currentDate = null;
            List<Orderlist> orderlistsNow = null;
            for (Orders ordersTemp:ordersList) {
                if(currentDate==null){
                    currentDate = ordersTemp.getOrdertime().toLocalDate();
                    orderlistsNow = new ArrayList<>();
                }
                else if(!currentDate.isEqual(ordersTemp.getOrdertime().toLocalDate())){
                    for(Item itemTemp:itemList){
                        Sells sells = new Sells();
                        sells.setSellnum(0);
                        for (Orderlist orderlist:orderlistsNow) {
//                            System.out.println(itemTemp.getItemid().toString().equals(orderlist.getItemid()));
                            if(itemTemp.getItemid().toString().equals(orderlist.getItemid())){
                                sells.setItemid(itemTemp.getItemid().toString());
                                sells.setSellnum(sells.getSellnum()+orderlist.getInum());
                                sells.setSelldate(currentDate);
                            }
                        }
                        if(sells.getItemid()!=null){
//                            System.out.println("-----------------------------------"+sells);
                            sellsService.save(sells);
                        }
                    }

                    currentDate = ordersTemp.getOrdertime().toLocalDate();
                    orderlistsNow = new ArrayList<>();
                }
                List<Orderlist> orderlists = orderlistService.list(new QueryWrapper<Orderlist>().eq("orderid",ordersTemp.getOrderid()));
//                orderlistsNow.add((Orderlist) orderlists);
                orderlistsNow.addAll(orderlists);
            }
            firstUpdate = true;
        }
        else{
                //执行初始更新
//            LocalDateTime localDateTime = LocalDateTime.now();
//            System.out.println("do else blanch");
            List<Orders> ordersList = ordersService.list(new QueryWrapper<Orders>().orderByAsc("ordertime"));
            List<Item> itemList = itemService.list(new QueryWrapper<Item>().groupBy("itemid"));
            LocalDate currentDate = LocalDate.now();
            List<Orderlist> orderlistsNow = new ArrayList<>();

            for (Orders ordersTemp:ordersList) {
                if(currentDate.isEqual(ordersTemp.getOrdertime().toLocalDate())) {
                    for (Item itemTemp : itemList) {
                        Sells sells = new Sells();
                        sells.setSellnum(0);
                        for (Orderlist orderlist : orderlistsNow) {
//                        System.out.println(itemTemp.getItemid().toString().equals(orderlist.getItemid()));
                            if (itemTemp.getItemid().toString().equals(orderlist.getItemid())) {
                                sells.setItemid(itemTemp.getItemid().toString());
                                sells.setSellnum(sells.getSellnum() + orderlist.getInum());
                                sells.setSelldate(currentDate);
                            }
                        }
                        if (sells.getItemid() != null) {
//                            System.out.println("-----------------------------------" + sells);
                            if(sellsService.getOne(new QueryWrapper<Sells>().eq("selldate",sells.getSelldate()))!= null){
//                                System.out.println("1");
                                sellsService.update(sells,new QueryWrapper<Sells>().eq("selldate",sells.getSelldate()));
                            }else {
//                                System.out.println("2");
                                sellsService.save(sells);
                            }

                        }
                    }
                    orderlistsNow = new ArrayList<>();
                    List<Orderlist> orderlists = orderlistService.list(new QueryWrapper<Orderlist>().eq("orderid", ordersTemp.getOrderid()));
                    orderlistsNow.addAll(orderlists);
                }
            }
        }
    }
}
